package cn.darkhorse.mesadmin.controller.statement;

import cn.darkhorse.common.util.ExcelTotalUtil;
import cn.darkhorse.device.dto.LintDetectorDTO;
import cn.darkhorse.device.mapper.HistoryScannerDayMapper;
import cn.darkhorse.device.pojo.IotPointTable;
import cn.darkhorse.device.vo.HistoryScannerDayVO;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/scanner")
@RestController
public class scannerController extends BaseController {
    @Resource
    private HistoryScannerDayMapper historyScannerDayMapper;

    /**
     * 查询
     *
     * @param dto
     * @return
     */
    @RequestMapping("/list")
    public TableDataInfo listAll(LintDetectorDTO dto) {
        /*if(StringUtils.isBlank(dto.getBeginDate())||StringUtils.isBlank(dto.getEndDate())){
            throw new ServiceException("请选择时间！");
        }*/
        if(StringUtils.isNotBlank(dto.getEndDate())&&StringUtils.isNotBlank(dto.getBeginDate())){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            LocalDate begindate=LocalDate.parse(dto.getBeginDate());
            LocalDate enddate=LocalDate.parse(dto.getEndDate());
            dto.setBeginDate(begindate.format(formatter));
            dto.setEndDate(enddate.format(formatter));
        }
        startPage();
        List<HistoryScannerDayVO> list = historyScannerDayMapper.listAll(dto);
        List<HistoryScannerDayVO> listTotal = historyScannerDayMapper.listAll(dto);
        Map<String, Object> total = ExcelTotalUtil.getTotal(listTotal);
        return getDataTable(list,total);
    }
    /**
     * 查询
     *
     * @param dto
     * @return
     */
    @RequestMapping("/export")
    public AjaxResult export(LintDetectorDTO dto) {
        /*if(StringUtils.isBlank(dto.getBeginDate())||StringUtils.isBlank(dto.getEndDate())){
            throw new ServiceException("请选择时间！");
        }*/
        if(StringUtils.isNotBlank(dto.getEndDate())&&StringUtils.isNotBlank(dto.getBeginDate())){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            LocalDate begindate=LocalDate.parse(dto.getBeginDate());
            LocalDate enddate=LocalDate.parse(dto.getEndDate());
            dto.setBeginDate(begindate.format(formatter));
            dto.setEndDate(enddate.format(formatter));
        }
        List<HistoryScannerDayVO> list = historyScannerDayMapper.listAll(dto);
        ExcelUtil<HistoryScannerDayVO> util = new ExcelUtil<>(HistoryScannerDayVO.class);
        return util.exportExcel(list, "皮棉加工数据报表","皮棉加工数据报表");
    }


}
